package club.zhcs.capture.ocr;

import static com.dgj.utils.file.Images.GetImageStr;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.nutz.http.Header;
import org.nutz.http.Request;
import org.nutz.http.Request.METHOD;
import org.nutz.http.Response;
import org.nutz.http.Sender;
import org.nutz.http.sender.PostSender;
import org.nutz.json.Json;
import org.nutz.lang.ContinueLoop;
import org.nutz.lang.Each;
import org.nutz.lang.ExitLoop;
import org.nutz.lang.Lang;
import org.nutz.lang.LoopException;
import org.nutz.lang.Strings;
import org.nutz.lang.Times;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Logs;

import club.zhcs.capture.ocr.BaiduOcr.IDCard.Sex;

/**
 * 百度OCR识别
 * 
 * @author Ixion
 *
 */
public class BaiduOcr {

	public static class IDCard {

		public static enum Sex {

			MALE("男"), FEMALE("女");
			private String name;

			public String getName() {
				return name;
			}

			public void setName(String name) {
				this.name = name;
			}

			private Sex(String name) {
				this.name = name;
			}

		}

		private String name;
		private Sex sex;
		private String volk;
		private Date birthDay;
		private String address;
		private String id;

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public Sex getSex() {
			return sex;
		}

		public void setSex(Sex sex) {
			this.sex = sex;
		}

		public String getVolk() {
			return volk;
		}

		public void setVolk(String volk) {
			this.volk = volk;
		}

		public Date getBirthDay() {
			return birthDay;
		}

		public void setBirthDay(Date birthDay) {
			this.birthDay = birthDay;
		}

		public String getAddress() {
			return address;
		}

		public void setAddress(String address) {
			this.address = address;
		}

		public String getId() {
			return id;
		}

		public void setId(String id) {
			this.id = id;
		}

		@Override
		public String toString() {
			return Json.toJson(this);
		}

	}

	/**
	 * ocr识别
	 * 
	 * @param path
	 *            图片文件路径
	 * @return 识别结果
	 */
	public static NutMap ocr(String path) {
		NutMap params = NutMap.NEW();
		Map<String, String> header = new HashMap<String, String>();
		header.put("apikey", "5f3fb5d03229507d70d6d6b165a5ec21");// appkey
																	// 目前是每天5000次的调用

		params.put("fromdevice", "pc");
		params.put("clientip", "10.0.1.1");
		params.put("detecttype", "LocateRecognize");
		params.put("languagetype", "CHN_ENG");
		params.put("imagetype", "1");
		params.put("image", GetImageStr(new File(path)));

		Request request = Request.create("http://apis.baidu.com/apistore/idlocr/ocr", METHOD.POST, params, Header.create(header));
		Sender sender = PostSender.create(request);
		Response response = sender.send();
		String info = response.getContent();

		return Lang.map(info);
	}

	/**
	 * 识别base64信息
	 * 
	 * @param imgStr
	 * @return
	 */
	public static NutMap ocrBase64(String imgStr) {
		NutMap params = NutMap.NEW();
		Map<String, String> header = new HashMap<String, String>();
		header.put("apikey", "5f3fb5d03229507d70d6d6b165a5ec21");// appkey
																	// 目前是每天5000次的调用
		params.put("fromdevice", "pc");
		params.put("clientip", "10.0.1.1");
		params.put("detecttype", "LocateRecognize");
		params.put("languagetype", "CHN_ENG");
		params.put("imagetype", "1");
		params.put("image", imgStr);

		Request request = Request.create("http://apis.baidu.com/apistore/idlocr/ocr", METHOD.POST, params, Header.create(header));
		Sender sender = PostSender.create(request);
		Response response = sender.send();
		String info = response.getContent();

		return Lang.map(info);
	}

	public static IDCard toCard(NutMap ocrResult) {
		final IDCard card = new IDCard();
		Lang.each(ocrResult.getList("retData", Map.class), new Each<Map>() {

			@Override
			public void invoke(int index, Map map, int length) throws ExitLoop, ContinueLoop, LoopException {
				NutMap data = NutMap.WRAP(map);
				String word = data.getString("word");
				if (word.indexOf("姓名") >= 0) {
					try {
						card.name = word.substring(word.indexOf("姓名") + 2);
					} catch (Exception e) {
						Logs.get().equals(e);
					}
				} else if (word.indexOf("性别") >= 0 || word.indexOf("民族") >= 0) {
					card.sex = word.indexOf("男") >= 0 ? Sex.MALE : Sex.FEMALE;
					if (word.indexOf("族") >= 0) {
						try {
							card.volk = word.substring(word.indexOf("族") + 1);
						} catch (Exception e) {
							Logs.get().equals(e);
						}
					}
				} else if (word.indexOf("出生") >= 0) {
					try {
						card.birthDay = Times.D(word.substring(word.indexOf("出生") + 2).replace("年", "-").replace("月", "-").replace("日", "").replace("[\u4E00-\u9FA5]+", "").trim());
					} catch (Exception e) {
						Logs.get().equals(e);
					}
				} else if (word.indexOf("身份号码") >= 0) {
					try {
						card.id = word.substring(word.indexOf("身份号码") + 4);
					} catch (Exception e) {
						Logs.get().equals(e);
					}
				} else if (word.indexOf("住址") >= 0) {
					if (Strings.isBlank(card.address)) {
						try {
							card.address = word.substring(word.indexOf("住址") + 2);
						} catch (Exception e) {
							Logs.get().equals(e);
						}
					} else {
						try {
							card.address = card.address = word.substring(word.indexOf("住址") + 2) + card.address;
						} catch (Exception e) {
							Logs.get().equals(e);
						}
					}
				} else {
					if (Strings.isBlank(card.address)) {
						card.address = word;
					} else {
						card.address += word;
					}
				}
			}
		});
		return card;
	}

}
